package cn.lliiooll.ippzonetoolbox.ui.activities.find;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import cn.lliiooll.ippzonetoolbox.R;
import cn.lliiooll.ippzonetoolbox.data.PData;
import cn.lliiooll.ippzonetoolbox.modules.ip.IpResultData;
import cn.lliiooll.ippzonetoolbox.task.PTaskBuilder;
import cn.lliiooll.ippzonetoolbox.ui.activities.input.InputActivity;
import cn.lliiooll.ippzonetoolbox.utils.ActivitiesManager;
import cn.lliiooll.ippzonetoolbox.utils.IppzoneUtils;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import lombok.Getter;

@Route(path = "/ipp/find")
public class FindActivity extends AppCompatActivity {
    @Getter
    public static FindActivity instance;

    @Autowired(name = "ip")
    public String ip;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        instance = this;
        ARouter.getInstance().inject(this);
        setContentView(R.layout.activity_find);
        ActivitiesManager.add(this);
        final TextView find1 = findViewById(R.id.find_text);// 状态
        final TextView find2 = findViewById(R.id.find_text2);// 国家
        final TextView find3 = findViewById(R.id.find_text3);// 省市
        final TextView find4 = findViewById(R.id.find_text4);// 城市
        final TextView find5 = findViewById(R.id.find_text5);// 纬度
        final TextView find6 = findViewById(R.id.find_text6);// 经度
        final TextView find7 = findViewById(R.id.find_text7);// ip
        final TextView find8 = findViewById(R.id.find_text8);// 时区
        final TextView find9 = findViewById(R.id.find_text9);// 运营商
        final TextView find10 = findViewById(R.id.find_text10);// 区运营商
        final PData<IpResultData> data = new PData<>();
        PTaskBuilder.builder()
                .setLabel("launcher")
                .setRunnable(new Runnable() {
                    @Override
                    public void run() {
                        FindActivity.getInstance().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(FindActivity.getInstance(), ip, Toast.LENGTH_SHORT).show();
                            }
                        });
                        data.setData(IppzoneUtils.getIpData(ip));
                    }
                })
                .onFinished(new Runnable() {
                    @Override
                    public void run() {
                        final IpResultData d = data.getData();
                        FindActivity.getInstance().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                find1.setText("状态：" + d.getStatus());
                                find2.setText("国家：" + d.getCountry());
                                find3.setText("省市：" + d.getRegionName());
                                find4.setText("城市：" + d.getCity());
                                find5.setText("纬度：" + d.getLat());
                                find6.setText("经度：" + d.getLon());
                                find7.setText("ip：" + d.getQuery());
                                find8.setText("时区：" + d.getTimezone());
                                find9.setText("运营商：" + d.getIsp());
                                find10.setText("区运营商：" + d.getOrg());
                            }
                        });
                    }
                }).start();
    }
}
